import org.junit.jupiter.api.Test;

public class _12_整数转罗马数字 {
    @Test
    void test(){

        System.out.println(intToRoman(2000));
    }

    public String intToRoman(int num) {
        //定义所有可能的数字
        int [] nums={1000,900,500,400,100,90,50,40,10,9,5,4,1};
        String[] num_str={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        StringBuilder s=new StringBuilder();
        //遍历nums，把每一次小于num的数都减掉，然后拼接字符串，直到为零输出罗马数字
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]<=num){
                num-=nums[i];
                s.append(num_str[i]);
                    i--;

            }
            if (num==0){
                break;
            }
        }
        return s.toString();
    }
}
